/*
 	Tool for Path Finding Algorithm Testing (ToPFAT) 
 	
    Copyright (C) 2009, Teemu Turunen and Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Tool for Path Finding Algorithm Testing.

    Tool for Path Finding Algorithm Testing is free software: you can 
    redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Tool for Path Finding Algorithm Testing is distributed in the hope that it
    will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         <http://www.gnu.org/licenses/>.
 */

package pathfinding.gui.listeners;

import java.awt.Dimension;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

import pathfinding.gui.Map;
import pathfinding.gui.PathFindingApplet;

public class ZoomWheelListener implements MouseWheelListener {

	private static final int MAP_ZOOM = 50;

	private PathFindingApplet master;
	
	public ZoomWheelListener(PathFindingApplet master) {
		this.master = master;
	}
	
	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		
		int zoomAmount = MAP_ZOOM;
		
		int notches = e.getWheelRotation();
		if (notches > 0) {
			zoomAmount *= -1;
		}
		else {
			zoomAmount = Math.abs(zoomAmount);
		}
		
		JTabbedPane tabPane = this.master.getTabbedPane();
		if (! (tabPane.getSelectedComponent() instanceof JScrollPane)) {
			return;
		}
		
		JScrollPane jscroll = (JScrollPane)tabPane.getSelectedComponent();
		if (! (jscroll.getViewport().getView() instanceof Map)) {
			return;
		}
		
		Map map = (Map) jscroll.getViewport().getView();
		
		Dimension size = map.getSize();
		int width = (int)size.getWidth();
		int height = (int)size.getHeight();
		
		// to prevent setting map size too small
		if (zoomAmount < 0 && width < jscroll.getVisibleRect().getWidth() && 
			height < jscroll.getVisibleRect().getHeight()) {
			return;
		}

		size.setSize(width + zoomAmount, height + zoomAmount);
		map.setPreferredSize(size);
		map.setSize(size);
		map.validate();	
		
	}



}
